//计算时间

import javax.crypto.Mac;

public class TimeLine {
    //DIFS
    public static final int DIFS = 1;
    //帧的固定传输时间
    public static final int TRANSMISSION_DURATION = 80;
    //超时时间
    public static final int TRANSMISSION_TIMEOUT = 81;

    public static final int NUM_TRANS_DRT = 10000;

    public static long loopNum = TRANSMISSION_DURATION * NUM_TRANS_DRT;

    static long current = 0L;

    int framesNumPerStation = DistributeGenerator.MaxFramePerStation;
    public void start() {
        MacChannel macChannel = new MacChannel();
        Station[] stations = new Station[5];
        for (int s = 0; s < 5; s++) {
            stations[s] = new Station(s);
            macChannel.addStation(stations[s]);
        }
        int[][] frameArriveArrays = new int[5][framesNumPerStation];
        for (int a = 0; a < 5; a++) {
            frameArriveArrays[a] = DistributeGenerator.getPossionList();
        }
        int[] pointer = {0, 0, 0, 0, 0};
        //开始循环仿真
        for (; current < loopNum; current++) {
            //向站中添加上一个时间槽新生成的Frame
            for (int a = 0; a < 5; a++) {
                //所有帧都到达了
                if (pointer[a] >= framesNumPerStation) {
                    continue;
                }
                if (frameArriveArrays[a][pointer[a]] < current) {
                    Frame frame = new Frame(stations[a]);
                    frame.setGenerateTime(current);
                    stations[a].addFrame(frame);
                    pointer[a]++;
                }
            }
            //监听信道,若正在传输,执行传输并跳过下述步骤
            if (macChannel.doTransmission()) {
                continue;
            }
            //若信道空闲,各个站检查自身,执行routine:进行回退或发送帧或等待
            for (int s = 0; s < 5; s++) {
                stations[s].routine(macChannel);
            }
            //发送帧阶段结束后,信道检测是否开始传输
            macChannel.startTransmission();
        }

        //打印站数据
        for (int a = 0; a < 5; a++) {
            System.out.println("Station" + a +":");
            stations[a].stationRecorder.getDistribution();
        }
    }
}
